window.sessionStorage.setItem('vouchers-type',0);
var pro_load_page = 2,pro_load_flag = true;
var vouchers={
	init:function(data){
		app.ajax(
			'/Member/Coupon/index',
			data,
			function(res){
				if(res.code == 200){
					var type=window.sessionStorage.getItem('vouchers-type');
					if(type == 0){
						$(document).scrollTop(0);
						$('#tpl-voucher-list').show();
						app.tpl($('.voucher-list'),'tpl-voucher-list',res);
						$('.list .line:last').hide();
					}else if(type ==1){
						$(document).scrollTop(0);
						$('#tpl-voucher-list').hide();
						app.tpl($('.voucher-list'),'tpl-voucher-list1',res);
						$('.list').css(
							{
							'background':'url(../img/user/vouchers2.jpg) no-repeat left top',
							'width'     : '93%',
							'backgroundSize' : '100% 100%',
							'margin'    : '0 auto',
							'marginBottom' : '1rem'
							}
							);
						$('.list .line:last').hide();
					}else{
						$(document).scrollTop(0);
						$('#tpl-voucher-list').hide();
						app.tpl($('.voucher-list'),'tpl-voucher-list2',res);
						$('.list').css(
							{
								'background':'url(../img/user/vouchers2.jpg) no-repeat left top',
								'width'     : '93%',
								'backgroundSize' : '100% 100%',
								'margin'    : '0 auto',
								'marginBottom' : '1rem'
							}
						);
						$('.list .line:last').hide();
					}
				}
		});
	},
	loadMore : function(url,data){
		var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;" class="no-more">努力加载中~<div>';
   		$('.voucher-list').append(listLoadingEmpty);
		pro_load_flag = false;
		app.ajax(
			url,
			data,
			function(res){
				$('.voucher-list .no-more').remove();
				if(res.code == 200){
					var list=res.list;
					if(res.list.length > 0){
						var type=window.sessionStorage.getItem('vouchers-type');
						if(type == 0){
							for(var i=0,html='';i< list.length;i++){
								html+="<div class='vouchers-list oF'><div class='fl pL10 quota'><p class='fS33 cWhite'><span class='fS12'>"
										+"&yen;</span>"+list[i].cpns_then+"</p></div><div class='fr pL10 pR10 w68'><div class='bBD pB7'>"
										+"<p class='mT19 cBlack fS12'>"+list[i].title+"</p><p class='cGrey fS12'><span class='b mT5 bR5 pL5 pR5'>"
										+"{{list.cpns_key}}</span><span>（可转赠好友使用）</span></p></div><div class='pT7 cGrey fS12 pB10'>"
										+"<span class='fS12 pR8'>剩余"+list[i].day+"天到期</span><span class='cRed fS12'>立即使用</span></div>"
										+"</div></div><div class='pB12 bT w93 mA'></div>";
							}
						}else{
							for(var i=0,html='';i< list.length;i++){
								var time =list[i].end_time;
								time =new Date(time);
								var m = time.getMonth()+1;
								m = 
								m <10 ? '0'+m :m;
								var t = time.getDate();
								t=
								 t<10 ? '0'+t : t;
								time = time.getFullYear()+'年'+m+'月'+t+'日';
								var pText = '已使用';
								if(type == 1){
									pText = '已使用';
								}else{
									pText = '已过期';
								}
								html+="<div class='list oF'><div class='fl pL10 quota'><p class='fS33 cWhite'><span class='fS12'>&yen;</span>"+list[i].cpns_then+"</p></div>"
									+"<div class='fr pL10 pR10 w68'><div class='bBD pB7'><p class='mT19 cBlack fS12'>"+list.title+"</p><p class='cGrey fS12'>"
									+"<span class='b mT5 bR5 pL5 pR5'>"+list[i].cpns_key+"</span></p></div><div class='pT7 cGrey fS12 pB10 oF'>"
									+"<span class='fS12 pR8 fl'>有效期至"+time+"</span><span class='cGrey fS12 show-title fr'>"+pText+"</span></div></div></div><div class='pB12 bT w93 mA line'></div>";
							}
						}
						$('.voucher-list').append(html);
						pro_load_page++;
						pro_load_flag = true;
					//page++;
					}else{						
						var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;">没有更多数据了~<div>';
						$('.voucher-list').append(listLoadingEmpty);
					}
				}else{
					layer.msg('系统繁忙，请稍候再试');
				}
			}
		);	
	}
}
	footer.init();
	header.init('代金劵');
	vouchers.init({
		status : 0,
		page :1,
		cpns_key : '' //搜索代金劵
	});

window.onload=function(){
	if($('header').html()){
		$('.explain').css('marginTop','7.34rem');
	}
	$('body').on('click','.voucher-list .voucher-use',function(){
		console.log(1);
		app.open('main.html','main',{},window.location.href,document.body.scrollTop);
	});
	
	$('.vouchers-nav li:first i').show();
	
	$('body').on('click','.vouchers-nav li',function(){
		$('.vouchers-nav li h2').removeClass('font-active');
		$('.vouchers-nav li i').hide();
		$(this).find('h2').addClass('font-active');
		$(this).find('i').show();
		var type=$(this).attr('data-type'),
		type1 = window.sessionStorage.getItem('vouchers-type');
		if(type == type1){
			return false;
		}else{
			window.sessionStorage.setItem('vouchers-type',type);
			var data={
				status : type,
				page   : 1,
				cpns_key : ''
			};
			pro_load_flag = true;
			$('.voucher-list .no-more').remove();
			vouchers.init(data);
			window.scrollTo(0,0);
		}
	});
	
	$(window).scroll(function(){
				
		if($('.voucher-list img').attr('src')){
			return false;
		}
		// 当滚动到距底部以上200像素
		var offset = $(".voucher-list").offset(),
		dataType = 0,
		data = {};
		var documentHeight = offset.top + $(".voucher-list").height();
		if (documentHeight - $(this).scrollTop() - $(this).height()<100){
				if(window.sessionStorage.getItem('vouchers-type')){
				 dataType=window.sessionStorage.getItem('vouchers-type');
				}
			data ={
				status : dataType,
				page   : pro_load_page,
				cpns_key : ''
			}
			pro_load_flag && vouchers.loadMore('/Member/Coupon/index',data);
		}
	});
	
	$('body').on('click','.explain',function(){
		layer.open({
			content:'1 每个订单只能使用一张代金券<br/>2 红人升级礼包不能使用代金券<br/>3 代金券不能提现，不能找零<br/>4 需要在有效期内使用，逾期作废',
			btn:['明白了'],
			shadeClose:false,
			yes:function(index){
				layer.close(index);
			}
		});
	});
};
